<?php
namespace app\common\model;
use think\Model;
use think\Db;
/**
 * 学科类目
 */
class Subject extends Model
{
    
    protected $append = [
        'knowledage_count',
        'exam_count'
    ];
    /*protected static function init()
    {
        Subject::event('before_delete', function ($subject) {
           $count =  Db::name('knowledge')->where('subject_id',$subject['id'])->count();
           if($count >0){
               return false;
           }
        });
    }*/
    public function getKnowledageCountAttr($value,$data){
        return Db::name('knowledge')->where('subject_id',$data['id'])->where('status',1)->count();
    }

    public function getExamCountAttr($value,$data){
       return Db::name('exam')->alias('e')->join('knowledge k','k.id=e.knowledge_id')->where('k.subject_id',$data['id'])->where('e.status',1)->where('k.status',1)->count('e.id');
    }
}